@ECHO OFF
setlocal  ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
::set Decrypt=Nothing
(set CHAR[UDFM45]=a) & (set CHAR[H21DGF]=b) & (set CHAR[FDH56D]=c) & (set CHAR[FGS546]=d) & (set CHAR[JUK4JH]=e)
(set CHAR[ERG54S]=f) & (set CHAR[T5H4FD]=g) & (set CHAR[RG641G]=h) & (set CHAR[RG4F4D]=i) & (set CHAR[RT56F6]=j)
(set CHAR[VCBC3B]=k) & (set CHAR[F8G9GF]=l) & (set CHAR[FD4CJS]=m) & (set CHAR[G423FG]=n) & (set CHAR[F45GC2]=o)
(set CHAR[TH5DF5]=p) & (set CHAR[CV4F6R]=q) & (set CHAR[XF64TS]=r) & (set CHAR[X78DGT]=s) & (set CHAR[TH74SJ]=t)
(set CHAR[BCX6DF]=u) & (set CHAR[FG65SD]=v) & (set CHAR[4KL45D]=w) & (set CHAR[GFH3F2]=x) & (set CHAR[GH56GF]=y)
(set CHAR[45T1FG]=z) & (set CHAR[D4G23D]=1) & (set CHAR[GB56FG]=2) & (set CHAR[SF45GF]=3) & (set CHAR[P4FF12]=4)
(set CHAR[F6DFG1]=5) & (set CHAR[56FG4G]=6) & (set CHAR[USGFDG]=7) & (set CHAR[FKHFDG]=8) & (set CHAR[IFGJH6]=9)
(set CHAR[87H8G7]=0) & (set CHAR[G25GHF]=@) & (set CHAR[45FGFH]=#) & (set CHAR[75FG45]=$) & (set CHAR[54GDH5]=*)
(set CHAR[45F465]=() & (set CHAR[HG56FG]=.) & (set CHAR[DF56H4]=,) & (set CHAR[F5JHFH]=-) & (set CHAR[SGF4HF]= )
(set CHAR[45GH45]=\) & (set CHAR[56H45G]=/)

SET mypath=%CD%\
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c%%a%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
IF "%mytime:~0,1%"==" " (SET mytime=0%mytime:~1%)
SET filename=%mydate%_%mytime%.fbk
SET logname=%mydate%_%mytime%.log
SET filezip=%mydate%_%mytime%.zip

::chuyen toi thu muc bin cua Firebird
FOR /F "eol=; tokens=1-5 delims=;" %%1 in (%mypath%DBconfig.txt) do (
     cd /d %%1
     set pwd=%%3
)

::decrypt
set Decrypt2=%pwd%
set "DecryptOut="
:decrypt2
set char=%Decrypt2:~0,6%
set Decrypt2=%Decrypt2:~6%
set DecryptOut=%DecryptOut%!CHAR[%char%]!
if not "%Decrypt2%"=="" goto decrypt2

set string=%DecryptOut%
set temp_str=%string%
set str_len=0
:lengthloop
if defined temp_str (
set temp_str=%temp_str:~1%
set /A str_len += 1
goto lengthloop )
echo.

::tao file backup
FOR /F "eol=; tokens=1-6 delims=;" %%1 in (%mypath%DBconfig.txt) DO (
start /wait "" gbak -v -t -user %%2 -password %DecryptOut% %%4 %%5\%filename% -y %%5\%logname%
if %ERRORLEVEL% NEQ 0 GOTO FAILED
if NOT EXIST %%5\%filename% GOTO FAILED
cd /d %mypath%
7za a -tzip %%5\%filezip% %%5\%filename%
del %%5\%filename%
goto END
)

::gui mail neu co loi xay ra
:FAILED
FOR /F "eol=; tokens=1-6 delims=;" %%1 in (%mypath%EmailConfig.txt) DO (
%mypath%mailsend1.17b14.exe -to %%1 -from %%2 -starttls -port 587 -auth -smtp smtp.gmail.com -sub %%5 +cc +bc -v -user %%3 -pass "%%4" -M %%6
)

:END